﻿CREATE TRIGGER [TRG_UpdateActivityOnCustomer] ON [tblCustomer] 
FOR UPDATE
AS
set nocount on
declare @CustomerID int

if update( DoNotProcess )
begin
	declare cAccountActivity cursor for
		select T1.CustomerID
		from deleted T1 join inserted T2 on T1.CustomerID = T2.CustomerID
		where coalesce( T1.DoNotProcess, 0 ) <> coalesce( T2.DoNotProcess, 0 )
	open cAccountActivity
	fetch next from cAccountActivity into @CustomerID
	while @@FETCH_STATUS = 0
	begin
		insert into tblAccountActivity ( CustomerID, KeyID, ActivityDate, ActivityCode, Agent )
		select @CustomerID, @CustomerID, GETDATE(), '19', dbo.CurrentUserID()

		fetch next from cAccountActivity into @CustomerID
	end
	close cAccountActivity
	deallocate cAccountActivity
end

